package test5.lupeng.jf.com.teamdemo.activitys;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import java.io.IOException;

import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.bean.RequestOtherMessage;
import test5.lupeng.jf.com.teamdemo.bean.response.ResponseOtherMessage;
import test5.lupeng.jf.com.teamdemo.untils.GlideCircleTransform;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;

//个人信息界面
public class PersonMessageActivity extends AppCompatActivity {

    private static final String TAG = "PersonMessageActivity";
    private ImageButton bt_return;
    private ImageView bt_addAttention;
    private TextView tv_score;
    private TextView tv_attentionNumber;
    private TextView tv_fensNumber;
    private LinearLayout tv_dongtaiNumber;
    private LinearLayout tv_bokeNumber;
    private LinearLayout tv_wendaNumber;
    private LinearLayout tv_taolunNumber;
    private ImageView peopleImage;
    private TextView peopleName;
    private TextView peopleWrite;
    private LinearLayout attention;
    private LinearLayout fens;
    private String otherUserId;
    private TextView tv_guanzhu;
    private LinearLayout linearAttention;
    private String userId;
    private Boolean followeeStatus = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_message);
        ButterKnife.bind(this);
        otherUserId = getIntent().getStringExtra("userId");

        initView();

        bt_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //请求其他用户的信息
        requestDate();

        //将点击事件设置到里面
        setClick();

    }

    public void requestDate(){
        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        //自己的id
        userId = sp.getString("objectId","0");

        RequestOtherMessage object = new RequestOtherMessage(userId, otherUserId);
        String url = "https://leancloud.cn:443/1.1/functions/getOthersInfo";
        Request request = HTTPUtils.requestPost(object, url);
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.e(TAG, "onResponse: "+result);
                final ResponseOtherMessage bean = new Gson().fromJson(result, ResponseOtherMessage.class);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Glide.with(PersonMessageActivity.this)
                             .load(bean.getResult().getAuthor().getHeadProtrait())
                             .centerCrop()
                             .transform(new GlideCircleTransform(PersonMessageActivity.this))
                             .into(peopleImage);

                        peopleName.setText(bean.getResult().getAuthor().getUsername());
                        peopleWrite.setText(bean.getResult().getAuthor().getSignature());


                        // ic_add_black_24dp  +
                        //ic_remove_black_24dp -
                        //拿到用户的关注状态
                        followeeStatus = bean.getResult().getAuthor().isFolloweeStatus();

                        Log.e(TAG, "run: followeeStatus"+followeeStatus);
                        //刚开始进去时的状态，以及文字的显示；
                        if(followeeStatus){

                            tv_guanzhu.setText("取消关注");
                            bt_addAttention.setImageResource(R.drawable.ic_remove_black_24dp);
                        }else{

                            tv_guanzhu.setText("关注");
                            bt_addAttention.setImageResource(R.drawable.ic_add_black_24dp);
                        }




                    }
                });

            }
        });

    }

    //关注的点击事件
    public void clickChange(View view){

            //关注状态
            if(followeeStatus){
                //取消关注
                deleteAttention();
                tv_guanzhu.setText("关注");
                bt_addAttention.setImageResource(R.drawable.ic_add_black_24dp);

            }else{
                //关注
                getAttention();
                tv_guanzhu.setText("取消关注");
                bt_addAttention.setImageResource(R.drawable.ic_remove_black_24dp);
            }

            followeeStatus = !followeeStatus;



    }


    //取消关注的方法
    public void deleteAttention(){
        String url = "https://api.leancloud.cn/1.1/users/" + userId + "/friendship/" + otherUserId;
        Request request = HTTPUtils.requestDelete(url);
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
               // Log.e(TAG, "onResponse: "+result);
                Log.e(TAG, "onResponse:取消关注成功  ");
            }
        });
    }

    //关注的方法
    public void getAttention(){
        String url = "https://api.leancloud.cn/1.1/users/" + userId + "/friendship/" + otherUserId;
        Request request = HTTPUtils.requestPostNull(url);
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.e(TAG, "onResponse: 关注成功");
            }
        });
    }





    //将点击事件设置到里面
    public void setClick() {

        //点击进入关注列表
        attention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PersonMessageActivity.this, AttentionActivity.class);
                intent.putExtra("otherId", otherUserId);
                startActivityForResult(intent,5);

            }
        });

        //点击进入粉丝列表
        fens.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PersonMessageActivity.this, FansActivity.class);
                intent.putExtra("otherId", otherUserId);
                startActivityForResult(intent,6);
            }
        });
    }



    public void initView(){
        bt_return = (ImageButton) findViewById(R.id.bt_return);

        //关注文字和图片的总布局
        linearAttention = (LinearLayout) findViewById(R.id.linearAttention);

        //关注的文字
        tv_guanzhu = (TextView) findViewById(R.id.tv_guanzhu);

        //"+" 号
        bt_addAttention = (ImageView) findViewById(R.id.bt_addAttention);

        //头像
        peopleImage = (ImageView) findViewById(R.id.peopleImage);

        //用户名
        peopleName = (TextView) findViewById(R.id.peopleName);

        //个性签名
        peopleWrite = (TextView) findViewById(R.id.peopleWrite);


        tv_score = (TextView) findViewById(R.id.tv_score);

        //点击进入关注界面
        attention = (LinearLayout) findViewById(R.id.attention);

        //点击进入粉丝的界面
        fens = (LinearLayout) findViewById(R.id.fens);


        //
        tv_attentionNumber = (TextView) findViewById(R.id.tv_attentionNumber);
        tv_fensNumber = (TextView) findViewById(R.id.tv_fensNumber);
        tv_dongtaiNumber = (LinearLayout) findViewById(R.id.tv_dongtaiNumber);
        tv_bokeNumber = (LinearLayout) findViewById(R.id.tv_bokeNumber);
        tv_wendaNumber = (LinearLayout) findViewById(R.id.tv_wendaNumber);
        tv_taolunNumber = (LinearLayout) findViewById(R.id.tv_taolunNumber);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 5:
                int guanzhuNumber =data.getIntExtra("guanzhuNumber",0);
                System.out.println(guanzhuNumber);
                tv_attentionNumber.setText(guanzhuNumber+"");
                break;
            case 6:
                int fansNumber = data.getIntExtra("fansNumber", 0);
                System.out.println(fansNumber);
                tv_fensNumber.setText(fansNumber+"");

                break;
            default:
        }
    }
}
